package com.example.learn.hwod.并查集;

public class UnionFind {
    private int count=0;
    private int[] parent;

    public UnionFind(int n){
        this.count=n;
        parent = new int[n];
        for (int i = 0; i < n; i++) {
            parent[i]=i;
        }
    }
    public int find(int p){
        while (p!=parent[p]){
            parent[p]=parent[parent[p]];
            p=parent[p];
        }
        return p;
    }
    public void union(int p, int q){
        int pRoot = find(p);
        int qRoot = find(q);
        if(pRoot==qRoot){
            return;
        }
        parent[pRoot]=qRoot;
        count--;
    }
}
